package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.util.ArrayList;
import java.util.List;

/**
 * 查询数据库
 * 1.封装sql语句
 */
public class UserImpl implements UserDao {
    @Override
    public int add(UserModel userModel) {
        String sql = "INSERT INTO `user` \n" +
                "(`username`, `password`, `sex` ,\n" +
                "`phone`, `email`, `address`, `create_time`,\n" +
                " `update_time`, `enable`)\n" +
                " VALUES (?, ?, ?," +
                "  ?, ?, ?, sysdate(), " +
                "now(), 1)";
//        ？叫占位符
//连接数据库---JDBC
//        JDBC的書寫步骤
        List<Object> list = new ArrayList<>();
        list.add(userModel.getUsername());
        list.add(userModel.getPassword());
        list.add(userModel.getSex());
        list.add(userModel.getPhone());
        list.add(userModel.getEmail());
        list.add(userModel.getAddress());
        return JDBC.update(sql, list);
    }

    @Override
    public int update(UserModel userModel) {
        String sql = " UPDATE `user` SET \n" +
                " `username` = ?, `password` = ?, \n" +
                " `sex` = ?,  \n" +
                " `phone` = ?, `email` = ?, `address` = ?,\n" +
                " `update_time` = now()  \n" +
                "  WHERE (`id` = ?)";
        List<Object> list = new ArrayList<>();
        list.add(userModel.getUsername());
        list.add(userModel.getPassword());
        list.add(userModel.getSex());
        list.add(userModel.getPhone());
        list.add(userModel.getEmail());
        list.add(userModel.getAddress());
        list.add(userModel.getId());
        return JDBC.update(sql, list);
    }
}
